package com.zhong.gang.applib.ui;

import android.content.Context;
import android.text.Editable;
import android.text.Layout;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.databinding.DataBindingUtil;

import com.zhong.gang.applib.R;
import com.zhong.gang.applib.databinding.LayoutZhangkouBinding;
import com.zhong.gang.applib.dialog.SlideDialog;
import com.zhong.gang.applib.dialog.ZhangKouDialog;
import com.zhong.gang.applib.utils.NumberUtils;

public class ZhangKouView extends FrameLayout implements RadioGroup.OnCheckedChangeListener
        , SlideDialog.onSlideListener<Integer>, TextWatcher {
    private LayoutZhangkouBinding binding;
    private ZhangKouDialog zhangKouDialog;
    private int status = 2;
    private boolean isCheckNone = true;
    private boolean clickedStaus = true;
    private double moneny;
    private ZhangKouListener listener;

    public void setListener(ZhangKouListener listener) {
        this.listener = listener;
    }

    public void setMoneny(double moneny) {
        this.moneny = moneny;
        afterTextChanged(null);
    }

    public ZhangKouView(Context context) {
        super(context);
        zhangKouDialog = new ZhangKouDialog(context);
        zhangKouDialog.setListener(this);
        addView();
    }

    public ZhangKouView(Context context, AttributeSet attrs) {
        super(context, attrs);
        zhangKouDialog = new ZhangKouDialog(context);
        zhangKouDialog.setListener(this);
        addView();
    }

    public ZhangKouView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        zhangKouDialog = new ZhangKouDialog(context);
        zhangKouDialog.setListener(this);
        addView();
    }

    private void addView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_zhangkou, null);
        binding = DataBindingUtil.bind(view);
        binding.monenyEdt.addTextChangedListener(this);
        binding.setView(this);
        binding.radio.setOnCheckedChangeListener(this);
        this.addView(view);
    }

    public LayoutZhangkouBinding getBinding() {
        return binding;
    }


    public void setAction(boolean status) {
        binding.radio.setEnabled(status);
        binding.zhangkouSelect.setEnabled(status);
        binding.monenyEdt.setEnabled(status);

    }

    public double getMoneny() {
        for (int n = 0; n < binding.radio.getChildCount(); n++) {
            RadioButton radioButton = (RadioButton) binding.radio.getChildAt(n);
            if (radioButton.isChecked() && n == 0) {
                return 0;
            }
        }
        double moneny = 0;
        try {
            moneny = Math.abs(Double.parseDouble(binding.monenyEdt.getText().toString()));
            if (status == 2) {
                moneny = -moneny;
            }
        } catch (Exception e) {
        }
        return NumberUtils.getNumber(moneny);
    }

    public void onViewClicked(View v) {
        zhangKouDialog.show();
    }

    public void setCheckStatus(boolean isHave) {
        ((RadioButton) binding.radio.getChildAt(isHave ? 1 : 0)).setChecked(true);
        binding.zhangkouSelectLine1.setVisibility(isHave ? VISIBLE : GONE);
        binding.zhangkouSelect.setVisibility(isHave ? VISIBLE : GONE);
        binding.zhangkouSelectLine0.setVisibility(isHave ? VISIBLE : GONE);
        binding.zhangkouMoneny.setVisibility(isHave ? VISIBLE : GONE);
    }

    public void setEnabled(boolean status) {
        clickedStaus = status;
        for (int n = 0; n < binding.radio.getChildCount(); n++) {
            View view = binding.radio.getChildAt(n);
            view.setEnabled(status);
        }
        binding.monenyEdt.setEnabled(status);
        binding.zhangkouSelect.setEnabled(status);
        binding.zhangkouMoneny.setEnabled(status);

//        binding.zhangkouMoneny.setVisibility(
//                clickedStaus ? GONE : (getMoneny() != 0 ? VISIBLE : GONE));
//        binding.zhangkouSelect.setVisibility(
//                clickedStaus ? GONE : (getMoneny() != 0 ? VISIBLE : GONE));
//        binding.zhangkouSelectLine0.setVisibility(
//                clickedStaus ? GONE : (getMoneny() != 0 ? VISIBLE : GONE));
//        binding.zhangkouSelectLine1.setVisibility(
//                clickedStaus ? GONE : (getMoneny() != 0 ? VISIBLE : GONE));
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        for (int n = 0; n < radioGroup.getChildCount(); n++) {
            RadioButton radioButton = (RadioButton) radioGroup.getChildAt(n);
            if (radioButton.isChecked()) {
                isCheckNone = n == 0;
                binding.zhangkouSelectLine1.setVisibility(n == 1 ? VISIBLE : GONE);
                binding.zhangkouSelect.setVisibility(n == 1 ? VISIBLE : GONE);
                binding.zhangkouMoneny.setVisibility(n == 1 ? VISIBLE : GONE);
                binding.zhangkouSelectLine0.setVisibility(n == 1 ? VISIBLE : GONE);
                if (listener != null) {
                    if (n == 0) {
                        listener.onZhangKou(NumberUtils.getNumber(moneny));
                    } else {
                        listener.onZhangKou(NumberUtils.getNumber(moneny + getMoneny()));
                    }
                }
                return;
            }
        }
    }

    @Override
    public void onSlideBack(Integer data) {
        status = data + 1;
        binding.monenyEdt.setText("");
        binding.zhangkouType.setText(data == 0 ? "涨" : "扣");
        onCheckedChanged(binding.radio, 0);

    }

    public void setSlideText(Integer data) {
        status=data;
        binding.zhangkouType.setText(data == 1 ? "涨" : "扣");
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }


    public int getStatus() {
        return isCheckNone ? 0 : status;
    }

    @Override
    public void afterTextChanged(Editable s) {
        onCheckedChanged(binding.radio, 0);
    }

    public interface ZhangKouListener {
        void onZhangKou(double money);
    }
}
